package com.itwlj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itwlj.constant.MessageConstant;
import com.itwlj.dto.SubCategoryDTO;
import com.itwlj.dto.SubCategoryPageQueryDTO;
import com.itwlj.entity.SubCategory;
import com.itwlj.exception.DeletionNotAllowedException;
import com.itwlj.mapper.GoodsMapper;
import com.itwlj.mapper.SubCategoryMapper;
import com.itwlj.result.PageResult;
import com.itwlj.service.SubCategoryService;
import com.itwlj.vo.SubCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 子分类业务层
 */
@Service
public class SubCategoryServiceImpl implements SubCategoryService {

    @Autowired
    private SubCategoryMapper subCategoryMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 新增子分类
     *
     * @param subCategoryDTO
     */
    public void insert(SubCategoryDTO subCategoryDTO) {
        SubCategory subCategory = new SubCategory();
        BeanUtils.copyProperties(subCategoryDTO, subCategory);
        subCategoryMapper.insert(subCategory);
    }

    /**
     * 子分类分页查询
     *
     * @param subCategoryPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SubCategoryPageQueryDTO subCategoryPageQueryDTO) {
        PageHelper.startPage(subCategoryPageQueryDTO.getPage(), subCategoryPageQueryDTO.getPageSize());
        Page<SubCategoryVO> page = subCategoryMapper.pageQuery(subCategoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 子分类批量删除
     *
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        List<Long> goodsIds = goodsMapper.getGoodsIdsBySubCategoryIds(ids);

        if (!goodsIds.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.SUBCATEGORY_BE_RELATED_BY_GOODS);
        }

        subCategoryMapper.deleteBatch(ids);
    }

    /**
     * 根据id修改子分类
     *
     * @param subCategoryDTO
     */
    public void update(SubCategoryDTO subCategoryDTO) {
        SubCategory subCategory = new SubCategory();
        BeanUtils.copyProperties(subCategoryDTO, subCategory);
        subCategoryMapper.update(subCategory);
    }

    /**
     * 查询分类
     *
     * @return
     */
    public List<SubCategory> list() {
        List<SubCategory> subCategoryList = subCategoryMapper.list();
        return subCategoryList;
    }

    /**
     * 根据分类id查询子分类
     *
     * @param categoryId
     * @return
     */
    public List<SubCategory> getByCategoryId(Long categoryId) {
        List<SubCategory> subCategoryList = subCategoryMapper.getByCategoryId(categoryId);
        return subCategoryList;
    }

    /**
     * 根据id查询子分类
     *
     * @param id
     * @return
     */
    public SubCategory getById(Long id) {
        SubCategory subCategory = subCategoryMapper.getById(id);
        return subCategory;
    }
}
